package cn.skyquant.quant4j.service.boot.model.economics.jinshi;

import cn.skyquant.quant4j.api.dto.ResultDTO;
import cn.skyquant.quant4j.api.economics.EconomicsCategoryImpactDTO;
import cn.skyquant.quant4j.api.economics.EconomicsDTO;
import cn.skyquant.quant4j.api.economics.EconomicsEventDTO;
import cn.skyquant.quant4j.api.economics.EconomicsService;
import cn.skyquant.quant4j.sdk.util.bean.BeanTools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service("EconomicsServiceImpl")
public class EconomicsServiceImpl implements EconomicsService {

    @Resource
    EconomicsEventRepository economicsEventRepository;

    @Resource
    EconomicsRepository economicsRepository;
    
    @Resource
    EconomicsCategoryImpactRepository economicsCategoryImpactRepository;

    @Override
    public ResultDTO<List<EconomicsDTO>> findEconomicsByDate(Date publicTime) {
        List<Economics> economicsList = economicsRepository.findByPublicTime(publicTime);
        List<EconomicsDTO> economicsDTOList = batchConvertEconomicsToEconomicsDTO(economicsList);
        return ResultDTO.success(economicsDTOList);
    }

    @Transactional
    @Override
    public ResultDTO<List<EconomicsDTO>> findBetweenPublicTime(long from, long to) {
        List<Economics> economicsList = economicsRepository.findBetweenPublicTime(new Date(from),new Date(to));
        List<EconomicsDTO> economicsDTOList = new ArrayList<>();
        for(Economics economics:economicsList){
            if(economics.datanameId==null){
                continue;
            }
            Set<EconomicsCategoryImpact> categoryImpactSet = economicsCategoryImpactRepository.findByCategoryId(economics.datanameId);
            if(categoryImpactSet.isEmpty()){
                continue;
            }
            EconomicsDTO economicsDTO = new EconomicsDTO();
            BeanTools.copyPublicFields(economics,economicsDTO);
            for(EconomicsCategoryImpact economicsCategoryImpact:categoryImpactSet){
                EconomicsCategoryImpactDTO economicsCategoryImpactDTO = new EconomicsCategoryImpactDTO();
                BeanTools.copyPublicFields(economicsCategoryImpact,economicsCategoryImpactDTO);
                economicsDTO.impacts.add(economicsCategoryImpactDTO);
            }
            economicsDTOList.add(economicsDTO);
        }
        return ResultDTO.success(economicsDTOList);
    }

    @Override
    public ResultDTO<List<EconomicsEventDTO>> findEconomicsEventByDate(Date datetime) {
        List<EconomicsEvent> economicsEventList = economicsEventRepository.findByDatetime(datetime);
        List<EconomicsEventDTO> economicsEventDTOList = batchConvertEconomicsEventToEconomicsEventDTO(economicsEventList);
        return ResultDTO.success(economicsEventDTOList);
    }


    public static List<EconomicsDTO> batchConvertEconomicsToEconomicsDTO(List<Economics> list){
        return list.stream().map(EconomicsServiceImpl::convertEconomicsToEconomicsDTO).collect(Collectors.toList());
    }

    public static EconomicsDTO convertEconomicsToEconomicsDTO(Economics d){
        EconomicsDTO dto = new EconomicsDTO();
        BeanTools.copyPublicFields(d,dto);
        return dto;
    }

    public static List<EconomicsEventDTO> batchConvertEconomicsEventToEconomicsEventDTO(List<EconomicsEvent> list){
        return list.stream().map(EconomicsServiceImpl::convertEconomicsEventToEconomicsEventDTO).collect(Collectors.toList());
    }

    public static EconomicsEventDTO convertEconomicsEventToEconomicsEventDTO(EconomicsEvent d){
        EconomicsEventDTO dto = new EconomicsEventDTO();
        BeanTools.copyPublicFields(d,dto);
        return dto;
    }
}

